﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SFWS.XHandler.Core;
using JKF.Substructure;
using JKF.Entities;
using JKF.BusinessLayer.BLL;
using JKF.Entities.Model;
using System.Data;
namespace JKF.HandlerLayer.Gift
{
   public partial class Gift_TypeHandler:AHttpHandler
    {
       private static bllGIFT_TYPE bllGift_Type = new bllGIFT_TYPE();
        public override void ProcessRequest()
        {
            //所有错误和异常将抛出异常.正常执行程序则在方法则返回数据
            try
            {
                this.Manager(Utils.GetActionName);
            }
            catch (Exception ex)
            {
                //将错误抛到客户端(以Json格式)
                Utils.ToClient(true, ex.Message);
                //写日志
                AppLogger.Log(ex.Message, ex);
            }
        }

        /// <summary>
        /// 添加礼品类别
        /// </summary>
        public void InsertGift_Type()
        {
            try
            {
                var strScr = P("moduleObj");
                var gift_TypeInfo = this.BuildGIFT_TYPEinfoByJson(JsonSerializer.Deserialize<GIFT_TYPEinfo>(strScr), false);
                var tid = gift_TypeInfo.GIFT_TPYE_KEY;
                bllGift_Type.Add(gift_TypeInfo);
                var pk = gift_TypeInfo.GIFT_TPYE_KEY;
                Response.Write(JsonSerializer.Serialize(new ResponseInf { MessageCode = "Ok", Message = "添加成功.", Data = pk }));
                // 清除缓存
                CacheManage.Remove("GIFT_TYPEOne");
            }
            catch (Exception exp)
            {
                Response.Write(JsonSerializer.Serialize(new ResponseInf { MessageCode = "Error", Message = exp.Message.FilterJs() }));
            }
        }

        /// <summary>
        /// 修改礼品类别名称
        /// </summary>
        public void EditGift_Type()
        {
            try
            {
                var strScr = P("moduleObj");
                var gift_TypeInfo = this.BuildGIFT_TYPEinfoByJson(JsonSerializer.Deserialize<GIFT_TYPEinfo>(strScr), true);
                var tid = gift_TypeInfo.GIFT_TPYE_KEY;
                bllGift_Type.Update(gift_TypeInfo);
                var pk = gift_TypeInfo.GIFT_TPYE_KEY;
                Response.Write(JsonSerializer.Serialize(new ResponseInf { MessageCode = "Ok", Message = "修改成功.", Data = pk }));
                // 清除缓存
                CacheManage.Remove("GIFT_TYPEOne");
            }
            catch (Exception exp)
            {
                Response.Write(JsonSerializer.Serialize(new ResponseInf { MessageCode = "Error", Message = exp.Message.FilterJs() }));
            }
        }

        /// <summary>
        /// 删除礼品类别 
        /// </summary>
        public void DeleteGift_Type()
        {
            try
            {
                var gift_Type_key = P("id");
                bllGift_Type.Delete(new Guid(gift_Type_key));
                Response.Write(JsonSerializer.Serialize(new ResponseInf { MessageCode = "Ok", Data = gift_Type_key }));
                // 清除缓存
                CacheManage.Remove("GIFT_TYPEOne");
            }
            catch (Exception exp)
            {
                Response.Write(JsonSerializer.Serialize(new ResponseInf { MessageCode = "Error", Message = exp.Message.FilterJs() }));
            }
        }

        /// <summary>
        /// 获取礼品类别列表数据
        /// </summary>
        public void GetGift_Types()
        {
            var obj = new PagerList();

            int pageCount = 0;
            int rowCount = 0;

            obj.Entity = from a in bllGift_Type.GetGift_TypesBLL(P("NAME"), P("PageIndex").ToInt32(), P("PageSize").ToInt32(), ref pageCount, ref rowCount).AsEnumerable()
                         select new
                         {
                             GIFT_TPYE_KEY = a.Field<Guid>("GIFT_TPYE_KEY"),
                             NAME = a.Field<string>("NAME") 
                         };

            obj.RowCount = rowCount;
            obj.PageCount = pageCount;
            ResponseJsonObject = obj;
        }

        /// <summary>
        /// 从Json对象构建对象
        /// </summary>
        /// <param name="shopInfo">实体对象</param>
        /// <param name="isEdit">是否为编辑</param>
        /// <returns></returns>
        public GIFT_TYPEinfo BuildGIFT_TYPEinfoByJson(GIFT_TYPEinfo gift_TypeInfo, bool isEdit)
        {//SHOP_CODE, SHOP_NAME, LINKMAN, PHONE, AREA_CODE, ADDRESS, POSTAL_CODE, MEMO, SMS_AVAIBLE, SMS_SENDED, SINGNATURE, USER_CREATE, DATE_CREATE, DATE_UPDATE
            var entity = new GIFT_TYPEinfo();
            if (!isEdit) // 添加
            {
                entity.GIFT_TPYE_KEY = new Guid(DB.GetNewGuid(DB.HR)); //Guid.NewGuid();
            }
            else // 修改
            {
                entity.GIFT_TPYE_KEY = gift_TypeInfo.GIFT_TPYE_KEY;
            }
            entity.NAME = gift_TypeInfo.NAME; 
            entity.USER_CREATE = bllSYS_USER.CurrentUser.USER_KEY; // 获取当前用户guid
            entity.DATE_CREATE = System.DateTime.Now;
            return entity;
        }

        /// <summary>
        /// 根据模块group_code获得实体模块信息
        /// </summary>
        public void GetGift_TypeByCode()
        {
            try
            {
                var scid = P("moduleKey");
                Response.Write(JsonSerializer.Serialize(new ResponseInf { MessageCode = "Ok", Data = bllGift_Type.GetModel(new Guid(scid)) }));
            }
            catch (Exception exp)
            {
                Response.Write(JsonSerializer.Serialize(new ResponseInf { MessageCode = "Error", Message = exp.Message.FilterJs() }));
            }
        }

        /// <summary>
        /// 检查已输入名称是否存在
        /// </summary>
        public void CheckGift_TypeName()
        {
            try
            {
                var gift_type_name = P("GIFT_TYPE_NAME");
                Response.Write(JsonSerializer.Serialize(new ResponseInf { MessageCode = "Ok", Data = bllGift_Type.ExistsGift_TypeNameBLL(gift_type_name) }));
            }
            catch (Exception exp)
            {
                Response.Write(JsonSerializer.Serialize(new ResponseInf { MessageCode = "Error", Message = exp.Message.FilterJs() }));
            }
        }
    }
}
